package cn.itcast.controller;


import cn.itcast.domain.Courier;
import cn.itcast.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class CourierController {

    @Autowired
    private CourierService courierService;

    @RequestMapping("/courier_save")
    public String save(Courier courier) {
        System.out.println(courier);
        courierService.save(courier);
        return "redirect:/pages/base/courier.html";
    }

    @RequestMapping("/courier_pageQuery")
    @ResponseBody
    public Map pageQuery(int page,int rows){
        //调用业务层,查询数据结果
        PageRequest pageable = PageRequest.of(page - 1, rows);
        Page<Courier> courierPage=courierService.queryPage(pageable);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("total",courierPage.getTotalElements()); //查询总记录
        map.put("rows",courierPage.getContent());//每页显示多少条记录
        return map;
    }

    @RequestMapping("/courier_delBatch")
    public String delBatch(String ids){
        //将前端传过来的id数组按","进行切割
        String[] idArray = ids.split(",");
        //调用作废的方法
        courierService.delBatch(idArray);
        return "redirect:/pages/base/courier.html";
    }

    @RequestMapping("/courier_resBatch")
    public String resBatch(String ids){
        //将前端传过来的id数组按"," 进行切割
        String[] idArray = ids.split(",");
        courierService.resBatch(idArray);
        return "redirect:/pages/base/courier.html";
    }

    @RequestMapping("/courier_activeBatch")
    public String activeBatch(String ids){
        //将前端传过来的id数组按"," 进行切割
        String[] idArray = ids.split(",");
        courierService.activeBatch(idArray);
        return "redirect:/pages/base/courier.html";
    }
}
